function loadCompGrid( obj,node,parent )
%loads Computational Grids data from XML project file.

% This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2013. Jose Maria Garcia-Valdecasas Bernal

handles=guidata(obj.m_figure);

%node is the parent node (COMPUTATIONAL OR PARENT C. GRID).
%childNodes are the Computational Grids
childNodes=node.getChildNodes();
for j=0:childNodes.getLength()-1
    %get one grid
    child=childNodes.item(j);
    %retrieve name and type of grid
    name=char(child.getAttribute('Name'));
    %items are the properties of the grid
    items=child.getChildNodes();
    %let's check if the grid has nested grids
    nested=child.getElementsByTagName('NESTED');
    if nested.getLength>0
        %it has nested items...
        hasNested=1;
    else
        hasNested=0;
    end
    %data will be the grid data stored in the gridlist
    data=cell(items.getLength()-hasNested,4);
    for k=0:items.getLength()-1
        %store the info of the item in the data cell
        elem=char(items.item(k).getNodeName());
        if ~strcmp(elem,'NESTED')
            elem=regexprep(elem,'_',' ');
            text=char(items.item(k).getAttribute('txt'));
            kind=char(items.item(k).getAttribute('kind'));
            value=char(items.item(k).getTextContent());
            value=txt2data(value,kind);
            data(k+1,:)={elem,text,kind,value};
        end
    end
    
    model=get(obj.m_tree,'model');
    root=obj.m_tree.Root();
    %m_gridCount is an unique number for grids. It is initialized as 1.
    
    for l=0:get(root,'ChildCount')-1
        branch=root.getChildAt(l);
        kind=branch.getValue();
        kind=char(kind(2));
        if (strcmp(kind,'COMPUTATIONAL'))
            %we add grids in COMPUTATIONAL GRIDS branch.
            if strcmp(parent,'NONE')
                grid=branch;
                
                type=['cMain' num2str(obj.m_gridCount)];
                obj.m_gridCount=obj.m_gridCount+1;
            else
                grid=obj.getNodeByName(branch,parent);
                type=['cNest' num2str(obj.m_gridCount)];
                obj.m_gridCount=obj.m_gridCount+1;
            end
            
            branch=uitreenode('v0',{'unselected',type},name,[], false);
            model.insertNodeInto(branch,grid,grid.getChildCount());
            %generate data for propertygrid
            obj.m_grids.(type)=data;
            break;
        end
    end
    %draw grid in DisplayWidget
    handles.DisplayWidget.loadCompGrid(type);
    obj.updateDisplay(branch);
    
    if hasNested
        obj.loadCompGrid(nested.item(0),name);
    end
end

end

function data=txt2data(text,kind)

switch kind
    case 'num'
        data=str2double(text);
    case 'bool'
        if strcmp(text,'false')
            data=false;
        else
            data=true;
        end
    otherwise
        if ~isempty(str2num(text)) %returns value if it's numeric
            data=str2double(text);
        else
            data=text;
        end
end
end